{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用工具调用实现语义路由\n",
    "\n",
    "效果不如直接用提示词实现好。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "TOOLS = [\n",
    "    {\n",
    "        \"type\": \"function\",\n",
    "        \"function\": {\n",
    "            \"name\": \"问数智能体\",\n",
    "            \"description\": \"可以基于用户的问题生成SQL语句并在数据库中查询并返回结果.\",\n",
    "        },\n",
    "    },\n",
    "    {\n",
    "        \"type\": \"function\",\n",
    "        \"function\": {\n",
    "            \"name\": \"问答智能体\",\n",
    "            \"description\": \"可以基于用户的问题检索文档知识库并回答问题。\",\n",
    "        },\n",
    "    },\n",
    "]\n",
    "\n",
    "# 以下是为了更美观的输出\n",
    "def format_response(response):\n",
    "    \"\"\"格式化响应内容为更易读的格式\"\"\"\n",
    "    formatted_response = {\n",
    "        \"model\": response.model,\n",
    "        \"created_at\": response.created_at,\n",
    "        \"done\": response.done,\n",
    "        \"done_reason\": response.done_reason,\n",
    "        \"total_duration\": response.total_duration,\n",
    "        \"load_duration\": response.load_duration,\n",
    "        \"_evalprompt_count\": response.prompt_eval_count,\n",
    "        \"prompt_eval_duration\": response.prompt_eval_duration,\n",
    "        \"eval_count\": response.eval_count,\n",
    "        \"eval_duration\": response.eval_duration,\n",
    "        \"message\": {\n",
    "            \"role\": response.message.role,\n",
    "            \"content\": response.message.content,\n",
    "            \"images\": response.message.images,\n",
    "            \"tool_calls\": [\n",
    "                {\n",
    "                    \"function\": {\n",
    "                        \"name\": call.function.name,\n",
    "                        \"arguments\": call.function.arguments\n",
    "                    }\n",
    "                } for call in response.message.tool_calls\n",
    "            ] if response.message.tool_calls else None\n",
    "        }\n",
    "    }\n",
    "    return json.dumps(formatted_response, indent=4, ensure_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"model\": \"qwen2.5:14b\",\n",
      "    \"created_at\": \"2025-03-13T02:19:16.338235164Z\",\n",
      "    \"done\": true,\n",
      "    \"done_reason\": \"stop\",\n",
      "    \"total_duration\": 2049811784,\n",
      "    \"load_duration\": 32767569,\n",
      "    \"_evalprompt_count\": 206,\n",
      "    \"prompt_eval_duration\": 55000000,\n",
      "    \"eval_count\": 121,\n",
      "    \"eval_duration\": 1948000000,\n",
      "    \"message\": {\n",
      "        \"role\": \"assistant\",\n",
      "        \"content\": \"\",\n",
      "        \"images\": null,\n",
      "        \"tool_calls\": [\n",
      "            {\n",
      "                \"function\": {\n",
      "                    \"name\": \"问数智能体\",\n",
      "                    \"arguments\": {\n",
      "                        \"question\": \"今年春节假期期间大众加油站充电量趋势\"\n",
      "                    }\n",
      "                }\n",
      "            }\n",
      "        ]\n",
      "    }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "client = ollama.Client(host='http://192.168.20.43:11434')\n",
    "\n",
    "def llm_chat_tooluse(messages):\n",
    "    response = client.chat( # 仍然是用chat来调用\n",
    "        model='qwen2.5:14b', \n",
    "        tools=TOOLS,    # 添加工具列表\n",
    "        messages=[\n",
    "            {'role': 'user', 'content': messages}],\n",
    "    )\n",
    "    return response\n",
    "\n",
    "\n",
    "#测试\n",
    "messages = \"大众加油站寂今年春节期间充电量趋势\"\n",
    "response = llm_chat_tooluse(messages)\n",
    "formatted_output = format_response(response)\n",
    "print(formatted_output)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
